<?php

namespace App\Http\Controllers\Api;
use App\Model\BillModel;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

//发票
class BillController extends Controller
{
	//发票列表
	public function list(){
		//获取用户的发票信息
		$list = BillModel::getList(request()->uid);

		return ['code'=>1,'data'=>$list];
	}


	//添加发票
	public function insert(){
		DB::beginTransaction();
		try{
			//个人
			if(request()->type == 1){
				if(!request()->name){
					return ['code'=>0,'msg'=>retStrInfo(78)];
				}
				if(request()->is_default){
					$is_default = request()->is_default;
				}else{
					$is_default = 2;
				}
				if(request()->is_default == 1){
					BillModel::where('uid',request()->uid)->where('is_default',1)->where('type',1)->update(['is_default'=>2]);
				}
				BillModel::insert(1,request()->uid,request()->name,$is_default);
				//公司
			}else{
				if(!request()->name){
					return ['code'=>0,'msg'=>retStrInfo(78)];
				}
				if(!request()->duty){
					return ['code'=>0,'msg'=>retStrInfo(79)];
				}
				if(!request()->address){
					return ['code'=>0,'msg'=>retStrInfo(80)];
				}
				if(!request()->phone){
					return ['code'=>0,'msg'=>retStrInfo(81)];
				}
				if(!request()->bank){
					return ['code'=>0,'msg'=>retStrInfo(82)];
				}
				if(!request()->bank_num){
					return ['code'=>0,'msg'=>retStrInfo(83)];
				}
				if(request()->is_default){
					$is_default = request()->is_default;
				}else{
					$is_default = 2;
				}
				if(request()->is_default == 1){
					BillModel::where('uid',request()->uid)->where('is_default',1)->where('type',2)->update(['is_default'=>2]);
				}
				BillModel::insert(2,request()->uid,request()->name,$is_default,request()->duty,request()->address,request()->phone,request()->bank,request()->bank_num);
			}
			DB::commit();
			return ['code'=>1,'msg'=>retStrInfo(68)];
		}catch(Expectation $e){
			DB::rollBack();
			return ['code'=>1,'msg'=>retStrInfo(69)];
		}
	}

	//删除发票
	public function del(){
		if(!request()->id){
			return ['code'=>0,'msg'=>retStrInfo(76)];
		}
		DB::beginTransaction();
		try{
			$result = BillModel::del(request()->id);
			if($result == 2){
				return ['code'=>0,'msg'=>retStrInfo(73)];
			}
			DB::commit();
			return ['code'=>1,'msg'=>retStrInfo(72)];
		}catch(Expectation $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>retStrInfo(73)];
		}
	}

	//编辑发票
	public function edit(){
		$data = BillModel::list(request()->id);
		return ['code'=>1,'data'=>$data];
	}

	//编辑
	public function update_bill(){
		if(!request()->id){
			return ['code'=>0,'msg'=>retStrInfo(76)];
		}
		DB::beginTransaction();
		try{
			//个人
			if(request()->type == 1){
				if(!request()->name){
					return ['code'=>0,'msg'=>retStrInfo(78)];
				}
				if(request()->is_default){
					$is_default = request()->is_default;
				}else{
					$is_default = 2;
				}
				if(request()->is_default == 1){
					BillModel::where('uid',request()->uid)->where('type',1)->where('id','<>',request()->id)->update(['is_default'=>2]);
				}
				BillModel::upd_one(request()->id,request()->name,$is_default);
				//公司
			}else{
				if(!request()->name){
					return ['code'=>0,'msg'=>retStrInfo(78)];
				}
				if(!request()->duty){
					return ['code'=>0,'msg'=>retStrInfo(79)];
				}
				if(!request()->address){
					return ['code'=>0,'msg'=>retStrInfo(80)];
				}
				if(!request()->phone){
					return ['code'=>0,'msg'=>retStrInfo(81)];
				}
				if(!request()->bank){
					return ['code'=>0,'msg'=>retStrInfo(82)];
				}
				if(!request()->bank_num){
					return ['code'=>0,'msg'=>retStrInfo(83)];
				}
				if(request()->is_default){
					$is_default = request()->is_default;
				}else{
					$is_default = 2;
				}
				if(request()->is_default == 1){
//					dd(1);
					BillModel::where('uid',request()->uid)->where('type',2)->where('id','<>',request()->id)->update(['is_default'=>2]);

				}
				BillModel::upd_one(request()->id,request()->name,$is_default,request()->duty,request()->address,request()->phone,request()->bank,request()->bank_num);
			}
			DB::commit();
			return ['code'=>1,'msg'=>retStrInfo(74)];
		}catch(Expectation $e){
			DB::rollBack();
			return ['code'=>1,'msg'=>retStrInfo(75)];
		}
	}
}